package com.xudow.app.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.xudow.app.XApplication;
import com.xudow.app.ui.task.SessionTask;

import java.util.HashMap;

/**
 * Session Service 以保持session不过期
 *
 * @author lukecheng
 */
public class SessionService extends Service {
    private final int REQUEST_PERIOD = 1000 * 60 * 5; //5 minutes

    private Handler eventHandler;

    private XApplication application;

    public SessionService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();

        application = (XApplication) getApplication();

        eventHandler = new Handler();
        eventHandler.postDelayed(periodTask, REQUEST_PERIOD);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void retainSession() {
        SessionTask sessionTask = new SessionTask(getApplicationContext(), new Handler() {

        });
        sessionTask.execute(new HashMap<String, String>());
    }

    private Runnable periodTask = new Runnable() {
        public void run() {
            retainSession();

            eventHandler.postDelayed(periodTask, REQUEST_PERIOD);
        }
    };
}
